package com.iiitb.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.iiitb.model.SeedHandler;
import com.mysql.jdbc.Connection;

public class UserInterface extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	JButton enterSeed = new JButton("ENTER SEED");
	JButton startCrawl = new JButton("PROPAGATE TAGS");
	JButton query = new JButton("QUERY");
	JButton HITS = new JButton("QUERY BY HITS");
	JButton pageRank = new JButton("QUERY BY PAGERANK");

	JLabel userseed;
	JLabel userseed_wiki;
	JLabel query_website;
	JLabel init_tag;

	SeedHandler objSeedHandler = null;
	Connection conn = null;

	private JPanel contentPane;
	private JPanel contentPane1;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;

	public UserInterface() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		userseed = new JLabel("URL");
		userseed.setBounds(70, 89, 50, 25);
		contentPane.add(userseed);

		textField_1 = new JTextField();
		textField_1.setBounds(162, 89, 250, 25);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		userseed_wiki = new JLabel("Wiki Object");
		userseed_wiki.setBounds(70, 125, 70, 25);
		contentPane.add(userseed_wiki);

		textField_2 = new JTextField();
		textField_2.setBounds(162, 125, 250, 25);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		/*
		 * query_website = new JLabel("Query Website");
		 * query_website.setBounds(40, 180, 120, 25);
		 * contentPane.add(query_website);
		 * 
		 * textField_3 = new JTextField(); textField_3.setBounds(162, 180, 148,
		 * 20); contentPane.add(textField_3); textField_3.setColumns(10);
		 */
		init_tag = new JLabel("Initial tag");
		init_tag.setBounds(70, 180, 120, 25);
		contentPane.add(init_tag);

		textField_4 = new JTextField();
		textField_4.setBounds(162, 180, 250, 25);
		contentPane.add(textField_4);
		textField_4.setColumns(10);

		// creating the objects of SeedHandler
		try {
			objSeedHandler = new SeedHandler();
			objSeedHandler.loadData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		startCrawl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		startCrawl.setBounds(180, 250, 175, 23);
		contentPane.add(startCrawl);
		startCrawl.addActionListener(this);

		JLabel lblXcrawler = new JLabel("WEBSITE TAG PROPAGATION");
		lblXcrawler.setBounds(135, 30, 250, 14);
		contentPane.add(lblXcrawler);

		enterSeed.setBounds(50, 250, 125, 23);
		contentPane.add(enterSeed);
		enterSeed.addActionListener(this);

		query.setBounds(360, 250, 111, 23);
		contentPane.add(query);
		query.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {

		String tagResult;
		// Start crawling
		if (e.getSource() == startCrawl) {
			System.out.println("Webcrawling started!");
			// Map<String, String> propagate = new HashMap<>();

			try {
				if (!textField_1.getText().equals("")) {

					URL connect = new URL(textField_1.getText());
					URLConnection yc = connect.openConnection();
					Map<String, String> parseTag = new HashMap<>();
					String tag;
					parseTag.put(textField_1.getText(), "");
					if (textField_4.getText().equals("")) {
						tag = objSeedHandler.mainCrawler(parseTag);
						objSeedHandler.insertData(textField_1.getText(), tag);
						objSeedHandler.insertSearchedData(
								textField_1.getText(), tag);
					} else {
						objSeedHandler.insertData(textField_1.getText(),
								textField_4.getText());
						objSeedHandler.insertSearchedData(
								textField_1.getText(), textField_4.getText());
						textField_4.setText("");
					}
					textField_1.setText("");
				} else if (!textField_2.getText().equals("")) {
					textField_2.setText(textField_2.getText().toLowerCase());
					String[] split = textField_2.getText().split("\\s+");
					String str = null;
					String inString = "";
					for (String string : split) {
						str = Character.toString(string.charAt(0))
								.toUpperCase() + string.substring(1);
						str = str + "_";
						inString = inString + str;
					}

					inString = inString.substring(0, inString.length() - 1);
					String url = "http://en.wikipedia.org/wiki/" + inString;
					URL connect = new URL(url);
					URLConnection yc = connect.openConnection();
					Map<String, String> parseTag = new HashMap<>();
					String tag;
					parseTag.put(url, "");
					if (textField_4.getText().equals("")) {
						tag = objSeedHandler.mainCrawler(parseTag);
						objSeedHandler.insertData(url, tag);
						objSeedHandler.insertSearchedData(url, tag);
					} else {
						objSeedHandler.insertData(url, textField_4.getText());
						objSeedHandler.insertSearchedData(url,
								textField_4.getText());
						textField_4.setText("");
					}
					objSeedHandler.insertData(url, textField_4.getText());
					objSeedHandler.insertSearchedData(url,
							textField_4.getText());

					textField_2.setText("");

				}

				objSeedHandler.mainCrawler();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		else if (e.getSource() == query) {
			getQueryPane();
		} else if (e.getSource() == HITS) {

			try {
				objSeedHandler = new SeedHandler();
				tagResult = objSeedHandler.queryData(textField_3.getText(),
						"HITS");
				if (tagResult.equals("")) {
					tagResult = "No result found";
				}
				JOptionPane.showMessageDialog(this, tagResult);

			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		} else if (e.getSource() == pageRank) {

			try {
				objSeedHandler = new SeedHandler();
				tagResult = objSeedHandler.queryData(textField_3.getText(),
						"pageRank");
				if (tagResult.equals("")) {
					tagResult = "No result found";
				}
				JOptionPane.showMessageDialog(this, tagResult);

			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		}

		// Once user click this, the seed will be entered in DB
		else if (e.getSource() == enterSeed) {

			try {
				if (textField_1.getText().equals("")
						&& textField_2.getText().equals("")) {
					JOptionPane.showMessageDialog(contentPane,
							"Please enter a value ");
				} else if (textField_2.getText().equals("")) {

					URL connect = new URL(textField_1.getText());
					URLConnection yc = connect.openConnection();
					Map<String, String> parseTag = new HashMap<>();
					String tag;
					parseTag.put(textField_1.getText(), "");
					if (textField_4.getText().equals("")) {
						tag = objSeedHandler.mainCrawler(parseTag);
						objSeedHandler.insertData(textField_1.getText(), tag);
						objSeedHandler.insertSearchedData(
								textField_1.getText(), tag);
					} else {
						objSeedHandler.insertData(textField_1.getText(),
								textField_4.getText());
						objSeedHandler.insertSearchedData(
								textField_1.getText(), textField_4.getText());
						textField_4.setText("");
					}
					textField_1.setText("");
				} else {
					textField_2.setText(textField_2.getText().toLowerCase());
					String[] split = textField_2.getText().split("\\s+");
					String str = null;
					String inString = "";
					for (String string : split) {
						str = Character.toString(string.charAt(0))
								.toUpperCase() + string.substring(1);
						str = str + "_";
						inString = inString + str;
					}
					inString = inString.substring(0, inString.length() - 1);
					String url = "http://en.wikipedia.org/wiki/" + inString;
					URL connect = new URL(url);
					URLConnection yc = connect.openConnection();
					Map<String, String> parseTag = new HashMap<>();
					String tag;
					parseTag.put(url, "");
					if (textField_4.getText().equals("")) {
						tag = objSeedHandler.mainCrawler(parseTag);
						objSeedHandler.insertData(url, tag);
						objSeedHandler.insertSearchedData(url, tag);
					} else {
						objSeedHandler.insertData(url, textField_4.getText());
						objSeedHandler.insertSearchedData(url,
								textField_4.getText());
						textField_4.setText("");
					}
					textField_2.setText("");
					// WebGraph graph = new WebGraph();
					// PageRank pg = new PageRank(graph);
					// HITS hits = new HITS(graph);
				}

			} catch (Exception e1) {
e1.printStackTrace();
			}
		}

	}

	private void getQueryPane() {
		contentPane.setVisible(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 350);
		contentPane1 = new JPanel();
		contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane1);
		contentPane1.setLayout(null);

		query_website = new JLabel("Query Website");
		query_website.setBounds(70, 125, 120, 25);
		contentPane1.add(query_website);

		textField_3 = new JTextField();
		textField_3.setBounds(162, 125, 250, 25);
		contentPane1.add(textField_3);
		textField_3.setColumns(10);

		HITS.setBounds(80, 250, 140, 23);
		contentPane1.add(HITS);
		HITS.addActionListener(this);

		pageRank.setBounds(250, 250, 160, 23);
		contentPane1.add(pageRank);
		pageRank.addActionListener(this);

	}
}
